package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Role;
import com.qk.mapper.RoleMapper;
import com.qk.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;


    @Override
    public PageResult<Role> selectlist(String name, String label,
                                       Integer page, Integer pageSize) {
        //1、设置PageHelper分页参数
        PageHelper.startPage(page, pageSize);
        //2、调用mapper接口查询
        List<Role> roles = roleMapper.selectlist(name, label);
        //3、解析并封装分页结果
        Page<Role> p = (Page<Role>) roles;
        return new PageResult<>(p.getTotal(), p.getResult());
    }
//2、根据ID查询
    @Override
    public Role getById(Integer id) {
        Role role = roleMapper.selectById(id);
        System.out.println("查询角色：" + role);

        return role;
    }

    //3、添加角色
    @Override
    public void addRole(Role role) {
        //添加创建时间
        role.setCreateTime(LocalDateTime.now());
        //添加更新时间
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.insert(role);
        System.out.println("添加角色：" + role);

    }
    //4、删除角色
    @Override
    public void delete(Integer id) {
        roleMapper.deleteById(id);
        System.out.println("删除角色：" + id);

    }
    //5、修改角色
    @Override
    public void update(Role role) {
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.update(role);
        System.out.println("修改角色：" + role);

    }
//6、查询所有角色
    @Override
    public List<Role> getAllRoles() {
        return roleMapper.getAllRoles();
    }
}
